/* Some class variables */
var fieldDefaults = new Array();   // Store default values as arrays in here



/* Use jQuery's onLoad handler */
(function() {
	$(document).ready(function() {
		
		// Get the navigation elements to add hovers
		navLinks = $('#navigation ul li a');
		if(navLinks.size() > 0) {
			addHoverStates(72, 10, 0, navLinks);
		}
		
		// Get the login form to add focus/blur handlers
		loginForm = $('#login > fieldset');
		if(loginForm.size() > 0) {
			$(loginForm).children('p').children('.txt').each(function() {
				addTxtfldFocusHandler($(this));
			});
		}
	});

	/**
	  * Add hover states to the navigation items. These
	  * are done via sprites. The bottom half of the image
	  * represents the hover state.
	  * @param   spriteH   the height of the sprites
	  * @param   xOffset   the amount to offset backgrounds by on x axis
	  * @param   yOffset   the amount to offset backgrounds by on y axis
	  * @param   elements   the collection of elements to enhance
	  */
	function addHoverStates(spriteH, xOffset, yOffset, elements) {

		midY = Math.round(spriteH / 2) + yOffset;

		$(elements).hover(
			function() {
				$(this).css('background-position', xOffset+'px -'+midY+'px');
			},
			function() {
				$(this).css('background-position', xOffset+'px '+yOffset+'px');
			}
		);
	}

	/**
	  * Add a focus/blur handler to a text field. This
	  * takes the default value from the text field, stores it,
	  * and when the field has focus, clears it. When the field
	  * loses focus again, if it is empty, put the default
	  * back in. But don't overwrite user input.
	  * @param   txtElement   the form field to add this to
	  */
	function addTxtfldFocusHandler(txtElement) {
		// Store the default value
		defaultVal = $(txtElement).val();
		fieldDefaults[$(txtElement).attr('id')] = defaultVal;

		// Add focus handler
		$(txtElement).focus(function() {
			if($(this).val() == fieldDefaults[$(txtElement).attr('id')]) {
				$(this).val("");
			}
		});
		
		// Add blur handler
		$(txtElement).blur(function() {
			if($(this).val() == "") {
				$(this).val(fieldDefaults[$(txtElement).attr('id')]);
			}
		});
	}
})();